<?php

namespace App\Models;

use Session;

class MAdmin extends BaseModel
{
    public $table = "admin";    //指定表名

    /**********************表对应**************************/
    public function role()
    {
        return $this->hasOne(MRoles::class,'id', 'role_id');
    }

    /**
     * 管理员登录数据验证规则
     * $flag 0 = rules  1 = msg
     */
    public static function loginRulesWithMsg($flag)
    {
        $rules = [
            "name" => "required|alpha|between:3,50",
            'password' => ['regex:/^(^(?![^a-zA-Z]+$)(?!\D+$)(?![a-zA-Z0-9]+$).{10,}$)/'],
            'yzm' => 'required|captcha',
        ];

        $msg = [
            "name.required" => "请填写用户名!",
            "name.alpha" => "用户名只能为纯字母!",
            "name.between" => "用户名只能3-50位之间!",
            "password.regex" => "密码必须包含大小写字母、数字与特殊符号!",
            'yzm.required' => '验证码必须填写',
            'yzm.captcha' => '验证码输入不正确',
        ];

        if ($flag == 0) {
            return $rules;
        } else {
            return $msg;
        }
    }

    /** 登录账号密码验证  密码双重MD5加密
     * @param $request
     */
    public static function loginValid($request)
    {
        $admin = MAdmin::where(['name'=>$request->name, 'password'=>md5(md5($request->password))])->first();
        return $admin;
    }

    /**
     * 管理员登录状态
     */
    public function loginStatus($role,$bool = true)
    {
        if ($bool) {
            session([
                "admin_id" => $this->id,
                "admin_name" => $this->name,
                "admin_role_id" => $this->role_id,
                "admin_role_name" => $role->name
            ]);
        } else {
            session::forget(['admin_id',"admin_name","admin_role_id","admin_role_name"]);
        }

        return $this;
    }

    /**
     * admin表记录最后一次登录ip和时间
     */
    public function refreshIP($ip)
    {
        $this->last_ip = $ip;
        $this->last_time = time();

        return $this->save();
    }

    /**
     * 管理员保存数据验证规则
     * $flag 0 = rules  1 = msg
     */
    public static function saveRulesWithMsg($flag)
    {
        $rules = [
            "name" => "required|alpha|between:5,20",
            'password' => ['regex:/^(^(?![^a-zA-Z]+$)(?!\D+$)(?![a-zA-Z0-9]+$).{10,}$)/'],
            'role' => "required|numeric"
        ];

        $msg = [
            "name.required" => "请填写用户名!",
            "name.alpha" => "用户名只能为纯字母!",
            "name.between" => "用户名只能5-50位之间!",
            "password.regex" => "密码必须包含大小写字母、数字与特殊符号!",
            "role.required" => "请选择角色",
        ];

        if ($flag == 0) {
            return $rules;
        } else {
            return $msg;
        }
    }

    /**
     * 保存管理员
     */
    public function saveAdmin($request)
    {
        $this->name = $request->name;
        $this->password = md5(md5($request->password));
        $this->role_id = $request->role;

        return $this->save();
    }

    /** 检测用户名是否存在
     * $self 是否检测自己
     */
    public static function checkName($request,$self=true)
    {
        if ($self) {
            $admin = MAdmin::where('name',$request->name)->first();
        } else {
            $admin = MAdmin::where('name',$request->name)->where('id','!=',$request->id)->first();
        }

        return $admin;
    }
}